package request;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;

/**
 * 演示通过 request 对象获取常用信息
 */
public class GetCommonInfo extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获得访问的IP
        String ip = req.getRemoteHost();
        System.out.println(ip);

        //获得访问的方法
        String method = req.getMethod();
        System.out.println(method);

        //完成封IP的功能
        if(ip.startsWith("127"))
            resp.sendError(404,"Sorry! Your ip is invalid!");
        else
            resp.getWriter().write("Welcome! Your ip is valid!");
    }
}
